package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUser;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author hello
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements AgencyCertificationAuditService {
    @Autowired
    private AgencyCertificationMapper agencyCertificationMapper;

    @Override
    public PageResult<AgencyCertificationAuditResDTO> pageQuery(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAuditPageQueryReqDTO) {
        PageUtils.parsePageQuery(agencyCertificationAuditPageQueryReqDTO, AgencyCertificationAudit.class);
        Integer pageNo = Math.toIntExact(agencyCertificationAuditPageQueryReqDTO.getPageNo());
        Integer pageSize = Math.toIntExact(agencyCertificationAuditPageQueryReqDTO.getPageSize());
        Page<AgencyCertificationAudit> page = new Page<>(pageNo, pageSize);
        String name = agencyCertificationAuditPageQueryReqDTO.getName();
        String legalPersonName = agencyCertificationAuditPageQueryReqDTO.getLegalPersonName();
        Integer auditStatus = agencyCertificationAuditPageQueryReqDTO.getAuditStatus();
        Integer certificationStatus = agencyCertificationAuditPageQueryReqDTO.getCertificationStatus();
        this.lambdaQuery().like(ObjectUtil.isNotEmpty(name), AgencyCertificationAudit::getName, name)
            .like(ObjectUtil.isNotEmpty(legalPersonName), AgencyCertificationAudit::getLegalPersonName, legalPersonName)
            .eq(ObjectUtil.isNotEmpty(auditStatus), AgencyCertificationAudit::getAuditStatus, auditStatus)
            .eq(ObjectUtil.isNotEmpty(certificationStatus), AgencyCertificationAudit::getCertificationStatus, certificationStatus)
            .page(page);
        return PageUtils.toPage(page, AgencyCertificationAuditResDTO.class);
    }

    @Override
    @Transactional
    public void audit(Long id, Integer certificationStatus, String rejectReason) {

        UpdateWrapper<AgencyCertificationAudit> updateWrapper = new UpdateWrapper<>();
        CurrentUser currentUser = UserContext.currentUser();
        if (ObjectUtil.isNull(currentUser)) {
            throw new RuntimeException("获取当前用户失败");
        }
        //获取当前登录用户的id和姓名
        Long userId = currentUser.getId();
        String currentName = currentUser.getName();


        updateWrapper.lambda().eq(AgencyCertificationAudit::getId, id)
                     .set(AgencyCertificationAudit::getCertificationStatus, certificationStatus)
                     .set(AgencyCertificationAudit::getRejectReason, rejectReason)
                     .set(AgencyCertificationAudit::getUpdateTime, LocalDateTime.now())
                     .set(AgencyCertificationAudit::getAuditorName, currentName)
                     .set(AgencyCertificationAudit::getAuditorId, userId)

                     .set(AgencyCertificationAudit::getAuditStatus, 1);


        update(updateWrapper);
        //同时更新agency_certification表
        AgencyCertificationAudit getById = getById(id);
        Long serveProviderId = getById.getServeProviderId();
        UpdateWrapper<AgencyCertification> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.lambda().eq(AgencyCertification::getId, serveProviderId)
                      .set(AgencyCertification::getCertificationStatus, certificationStatus)
                      .set(AgencyCertification::getUpdateTime, LocalDateTime.now());
        agencyCertificationMapper.update(null, updateWrapper1);
    }
}
